// 防抖函数
let timeout: any;
function debounce(func: Function, wait = 300, immediate = false) {
  // 清空定时器
  if (timeout !== null) {
    clearTimeout(timeout);
  }
  if (immediate) {
    // 立即执行
    let callNow = !timeout;
    timeout = setTimeout(() => {
      timeout = null;
    }, wait);
    if (callNow) typeof func === "function" && func();
  } else {
    timeout = setTimeout(() => {
      typeof func === "function" && func();
    }, wait)
  }
}

// 返回当前时间对应的文字
const sayHello = (_hello: string) => {
  const hour = new Date().getHours();
  let hello = _hello;
  if (hour < 6) {
    hello = "凌晨好";
  } else if (hour < 9) {
    hello = "早上好";
  } else if (hour < 12) {
    hello = "上午好"
  } else if (hour < 14) {
    hello = "中午好";
  } else if (hour < 17) {
    hello = "下午好";
  } else if (hour < 19) {
    hello = "傍晚好";
  } else if (hour < 22) {
    hello = "晚上好";
  } else {
    hello = "夜深了";
  }
  ElMessage({
    dangerouslyUseHTMLString: true,
    message: `<strong>${hello}</strong>欢迎来到我的主页`,
  })
}

// 获取当前时间
const getCurrentTime = () => {
  const time = new Date();
  let year = time.getFullYear();
  let month = time.getMonth() +1 < 10 ? "0" + (time.getMonth() + 1) : time.getMonth() + 1;
  let day = time.getDay() < 10 ? "0" + (time.getDay()) : time.getDay();
  let hour = time.getHours() < 10 ? "0" + (time.getHours()) : time.getHours();
  let minute = time.getMinutes() < 10 ? "0" + (time.getMinutes()) : time.getMinutes();
  let second = time.getSeconds() < 10 ? "0" + (time.getSeconds()) : time.getSeconds();
  let weekday = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
  return {
    year,
    month,
    day,
    hour,
    minute,
    second,
    weekday: weekday[time.getDay()],
  };
}

export default function useUtils () {
  return {
    debounce,
    sayHello,
    getCurrentTime,
  }
};
